#region (c) 2014 THN Solutions LLC. - All rights reserved
/*
Simplified BSD License (BSD)
THN HTTP Stack Library
Copyright (c) 2014, THN Solutions LLC. ( www.ThnSolutions.com )
Author: Nguyễn, M. Hải                 ( www.Minh-Hai.com     )
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, this 
      list of conditions and the following disclaimer.

    * Redistributions in binary form must reproduce the above copyright notice, 
      this list of conditions and the following disclaimer in the documentation 
      and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
POSSIBILITY OF SUCH DAMAGE.
*/
#endregion

#region Record of Change
//             							CHANGE HISTORY
// -------------------------------------------------------------------------------------
// |   DATE    | DEVELOPER  | DESCRIPTION                                              |
// -------------------------------------------------------------------------------------
// | 04-Nov-12 | HaiNM      | First creation.                                          |
// -------------------------------------------------------------------------------------
//
#endregion

#region Record of Audit
//             							REVIEW HISTORY
// -------------------------------------------------------------------------------------
// |   DATE    | REVIEWER   | NOTES                                                    |
// -------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------
//
#endregion

#region Using directives
using System;

using Thn;
using Thn.IO;
#endregion

namespace Thn.Net.Http
{
    /// <summary>
    /// This class implements <see cref="IHttpBody"/> by using an internal <see cref="IConcurrentByteStream"/> to store data.
    /// </summary>
#if DEBUG || TESTING
    public
#else
    internal
#endif
 class ConcurrentHttpBody : IHttpBody
    {
        #region Fields
        ConcurrentByteStream mStream;
        #endregion

        #region Properties
        #endregion

        #region IHttpBody Members

        #region Writer
        ConcurrentContextWriter mWriter;
        /// <summary>
        /// Gets the writer for writing values to body section.
        /// </summary>
        IContextWriter IHttpBody.Writer
        {
            get
            {
                if (mWriter == null) mWriter = new ConcurrentContextWriter(mStream);
                #region Defensive tracing
                else
                {
                    //already initiated
                }
                #endregion

                return mWriter;
            }
        }
        #endregion

        #region Reader
        ConcurrentContextReader mReader;
        /// <summary>
        /// Gets the reader for writing values to body section.
        /// </summary>
        IContextReader IHttpBody.Reader
        {
            get
            {
                if (mReader == null) mReader = new ConcurrentContextReader(mStream);
                #region Defensive tracing
                else
                {
                    //already initiated
                }
                #endregion

                return mReader;
            }
        }
        #endregion

        #region Has Data
        /// <summary>
        /// Checks whether the body section has actual data.
        /// </summary>
        bool IHttpBody.HasData
        { get { return mStream.Length > 0; } }
        #endregion

        #region Length
        /// <summary>
        /// Gets number of byte the body section occupies.
        /// </summary>
        long IHttpBody.Length
        { get { return mStream.Length; } }
        #endregion

        #endregion

        #region Constructors
        /// <summary>
        /// Default constructor.
        /// </summary>
        public ConcurrentHttpBody()
        {
            mStream = new ConcurrentByteStream();
        }
        #endregion
    }
}
